/**
 * Copyright 2013 Viettel Telecom. All rights reserved.
 * VIETTEL PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.viettel.core.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Mo ta muc dich cua lop (interface)
 * 
 * @author: ToanTM1
 * @version: 2.1
 * @since: 20-09-2013 - 13:34:43
 */
public class ReExpUtil {
	private static Pattern pattern;
	private static Matcher matcher;
	private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
			+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

	private static final String USER_NAME = "^[a-z0-9_-]{3,15}$";

	private static final String PASS_WORD = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{8,20})";

	private static final String IMAGE_PATTERN = "([^\\s]+(\\.(?i)(jpg|png|gif|bmp))$)";

	public static boolean validate(String hex) {
		matcher = pattern.matcher(hex);
		return matcher.matches();
	}

	/**
	 * @author ToanTM1 20-09-2013 - 13:46:23 For: Mobile
	 * @description Check userName
	 */
	public static Boolean checkUserName(String userName) {
		pattern = Pattern.compile(USER_NAME);
		return validate(userName);
	}

	/**
	 * ( # Start of group (?=.*\d) # must contains one digit from 0-9
	 * (?=.*[a-z]) # must contains one lowercase characters (?=.*[A-Z]) # must
	 * contains one uppercase characters (?=.*[@#$%]) # must contains one
	 * special symbols in the list "@#$%" . # match anything with previous
	 * condition checking {8,20} # length at least 8 characters and maximum of
	 * 20 ) # End of group
	 */
	public static Boolean checkPassWord(String passWord) {
		pattern = Pattern.compile(PASS_WORD);
		return validate(passWord);
	}

	/**
	 * @author ToanTM1 20-09-2013 - 13:47:03 For: Mobile
	 * @description Check Email
	 */
	public static Boolean checkEmail(String email) {
		pattern = Pattern.compile(EMAIL_PATTERN);
		return validate(email);
	}
	/*
	(			#Start of the group #1
			 [^\s]+			#  must contains one or more anything (except white space)
			       (		#    start of the group #2
			         \.		#	follow by a dot "."
			         (?i)		#	ignore the case sensive checking for the following characters
			             (		#	  start of the group #3
			              jpg	#	    contains characters "jpg"
			              |		#	    ..or
			              png	#	    contains characters "png"
			              |		#	    ..or
			              gif	#	    contains characters "gif"
			              |		#	    ..or
			              bmp	#	    contains characters "bmp"
			             )		#	  end of the group #3
			       )		#     end of the group #2	
			  $			#  end of the string
			)			#end of the group #1
	*/
	public static Boolean checkImage(String imageName) {
		pattern = Pattern.compile(IMAGE_PATTERN);
		return validate(imageName);
	}
}
